package top.ccm.ems.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import top.ccm.ems.system.entity.*;
import top.ccm.ems.system.pojo.R;
import top.ccm.ems.system.service.*;
import top.ccm.ems.system.vo.QuestionVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ccm
 * @since 2021-11-18
 */
@Api(tags= {"题目管理"})
@RestController
@RequestMapping("/ems/question")
@CrossOrigin
public class QuestionController {

    @Resource
    private SingleChoiceQuestionService singleChoiceQuestionService;
    @Resource
    private MultipleChoicesQuestionService multipleChoicesQuestionService;
    @Resource
    private FillQuestionService fillQuestionService;
    @Resource
    private CaseQuestionService caseQuestionService;
    @Resource
    private TrueFalseQuestionService trueFalseQuestionService;

    @ApiOperation(value = "所有题列表")
    @GetMapping("/list")
    public R list(){
        List list1 = singleChoiceQuestionService.list(null);
        List list2 = multipleChoicesQuestionService.list(null);
        List list3 = caseQuestionService.list(null);
        List list4 = trueFalseQuestionService.list(null);
        List list5 = fillQuestionService.list(null);
        List<Question> questions = new ArrayList<>();
        questions.addAll(list1);
        questions.addAll(list2);
        questions.addAll(list3);
        questions.addAll(list4);
        questions.addAll(list5);
        return R.ok().data("data",questions);
    }

    @ApiOperation(value = "添加题目")
    @PostMapping("/add")
    public R add(@RequestBody QuestionVo question){
        switch (question.getType()){
            case "单选题":
                SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
                BeanUtils.copyProperties(question,singleChoiceQuestion);
                singleChoiceQuestionService.save(singleChoiceQuestion);
                break;
            case "多选题":
                MultipleChoicesQuestion multipleChoicesQuestion = new MultipleChoicesQuestion();
                BeanUtils.copyProperties(question,multipleChoicesQuestion);
                multipleChoicesQuestionService.save(multipleChoicesQuestion);
                break;
            case "案例题":
                CaseQuestion caseQuestion  = new CaseQuestion();
                BeanUtils.copyProperties(question,caseQuestion);
                caseQuestionService.save(caseQuestion);
                break;
            case "判断题":
                TrueFalseQuestion trueFalseQuestion = new TrueFalseQuestion();
                BeanUtils.copyProperties(question,trueFalseQuestion);
                trueFalseQuestionService.save(trueFalseQuestion);
                break;
            case "填空题":
                FillQuestion fillQuestion = new FillQuestion();
                BeanUtils.copyProperties(question,fillQuestion);
                fillQuestionService.save(fillQuestion);
                break;
        }
        return R.ok();
    }

    @ApiOperation(value = "编辑题目")
    @PostMapping("/edit")
    public R edit(@RequestBody QuestionVo question){
        switch (question.getType()){
            case "单选题":
                SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
                BeanUtils.copyProperties(question,singleChoiceQuestion);
                singleChoiceQuestionService.updateById(singleChoiceQuestion);
                break;
            case "多选题":
                MultipleChoicesQuestion multipleChoicesQuestion = new MultipleChoicesQuestion();
                BeanUtils.copyProperties(question,multipleChoicesQuestion);
                multipleChoicesQuestionService.updateById(multipleChoicesQuestion);
                break;
            case "案例题":
                CaseQuestion caseQuestion  = new CaseQuestion();
                BeanUtils.copyProperties(question,caseQuestion);
                caseQuestionService.updateById(caseQuestion);
                break;
            case "判断题":
                TrueFalseQuestion trueFalseQuestion = new TrueFalseQuestion();
                BeanUtils.copyProperties(question,trueFalseQuestion);
                trueFalseQuestionService.updateById(trueFalseQuestion);
                break;
            case "填空题":
                FillQuestion fillQuestion = new FillQuestion();
                BeanUtils.copyProperties(question,fillQuestion);
                fillQuestionService.updateById(fillQuestion);
                break;
            default:
                return R.error().message("查无此题");
        }
        return R.ok();
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除")
    @DeleteMapping("delete")
    public R delete(@RequestParam(name = "id",required = false) String id,
                    @RequestParam(name = "type",required = false) String type) {
        switch (type){
            case "单选题":
                SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
                singleChoiceQuestionService.removeById(singleChoiceQuestion.setId(id));
                break;
            case "多选题":
                MultipleChoicesQuestion multipleChoicesQuestion = new MultipleChoicesQuestion();
                multipleChoicesQuestionService.removeById(multipleChoicesQuestion.setId(id));
                break;
            case "案例题":
                CaseQuestion caseQuestion  = new CaseQuestion();
                caseQuestionService.removeById(caseQuestion.setId(id));
                break;
            case "判断题":
                TrueFalseQuestion trueFalseQuestion = new TrueFalseQuestion();
                trueFalseQuestionService.removeById(trueFalseQuestion.setId(id));
                break;
            case "填空题":
                FillQuestion fillQuestion = new FillQuestion();
                fillQuestionService.removeById(fillQuestion.setId(id));
                break;
            default:
                return R.error().message("查无此题");
        }
        return R.ok().message("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestParam(name = "ids") String[] ids,String[] types) {
        for (int i = 0; i < ids.length; i++) {
            String id = ids[i];
            switch (types[i]){
                case "单选题":
                    SingleChoiceQuestion singleChoiceQuestion = new SingleChoiceQuestion();
                    singleChoiceQuestionService.removeById(singleChoiceQuestion.setId(id));
                    break;
                case "多选题":
                    MultipleChoicesQuestion multipleChoicesQuestion = new MultipleChoicesQuestion();
                    multipleChoicesQuestionService.removeById(multipleChoicesQuestion.setId(id));
                    break;
                case "案例题":
                    CaseQuestion caseQuestion  = new CaseQuestion();
                    caseQuestionService.removeById(caseQuestion.setId(id));
                    break;
                case "判断题":
                    TrueFalseQuestion trueFalseQuestion = new TrueFalseQuestion();
                    trueFalseQuestionService.removeById(trueFalseQuestion.setId(id));
                    break;
                case "填空题":
                    FillQuestion fillQuestion = new FillQuestion();
                    fillQuestionService.removeById(fillQuestion.setId(id));
                    break;
                default:
                    return R.error().message("查无此题");
            }
        }
        return R.ok().message("批量删除成功！");
    }
}

